<?php
/**
 * 操作用户信息
 * User: 老毛
 * Date: 2019/9/4
 * Time: 10:03
 */

namespace app\api\controller;
use function PHPSTORM_META\type;
use think\Db;
use think\Validate;
use app\api\logic\Sms;
class SetUserinfo extends Base
{
    //登录接口
    public function mk_login(){
       $post = $_POST;
       $rule = [
           'username'            => 'require|[1][3-9][0-9]{9}',
           'password'            => 'require|length:6,15|alphaNum',
           'appid'            => 'require',
           'clientid'            => 'require',
            'version'            => 'require',
       ];

       $msg = [
           'username.require'           => '手机号不能为空',
           'username'           => '请输入正确的手机号',
           'password.require'    => '密码不能为空',
           'password.length'    => '密码长度为6到15位',
           'password.alphaNum'    => '密码只能是字母和数字',
           'appid.require'           => '缺少appid参数',
           'clientid.require'           => '缺少clientid参数',
           'version.require'           => '缺少version参数',
       ];
       $validate = new Validate($rule, $msg);
       if(!$validate->check($post))
       {
           return ajaxReturn($validate->getError(),99);
       }
       //验证账号是否存在和密码是否匹配
       $userinfo = Db::name('php_member')->where(['username'=> $post['username'],'password' => md5($post['password'])])->find();
       if(empty($userinfo)) return ajaxReturn('账号或密码错误',99);
       //查看当前用户是否被禁用
       if($userinfo['status']) return ajaxReturn('账号已被冻结',99);
       //设置当前登录方式为手机号登陆
        $userinfo['logincode'] = 1;
       //生成token
       $arr = [
           'uid' => $userinfo['id'],
           'appid'=>$post['appid'],
           'clientid'=>$post['clientid'],
           'version'=>$post['version']
       ];
       $data = self::setToken($arr);
       if($data['code']){
           $dataResult = [
               'userinfo' => $userinfo,
               'token' => $data['data']
           ];
           return ajaxReturn('登录成功',1,$dataResult);
       }else{
           return ajaxReturn('登录失败',0);
       }
   }
   //注册获取验证码
   public function getCode(){
        //得到电话号码
       $mobile = $_POST['phone'];
       //验证电话号码是否正确
       if(!preg_match('/^1[3-9][0-9]{9}/',$mobile)) return ajaxReturn('请输入正确的手机号',0);
       //生成4位随机数
       $dataCode = mt_rand(1000,9999);
       //开始发送短信
     $result = \app\api\logic\Sms::sendSMS("{$mobile}","9639",[$dataCode,'3']);
     if($result == 'ok'){
         //存入数据库
         $dataphone = Db::name('php_sms')->insert(['code'=> $dataCode,'phone'=>$mobile]);
         if (!$dataphone) return ajaxReturn('验证码发送失败！',0);
         return ajaxReturn('验证码发送成功',1);
     }else{
         return ajaxReturn('验证码发送失败！',0);
     }
   }

   //app注册
    public function register(){
        $post = $_POST;
        //验证数据
        $rule = [
            'username'            => 'require|unique:php_member|[1][3-9][0-9]{9}',
            'code'            => 'require',
            'password'            => 'require|length:6,15|confirm',
        ];

        $msg = [
            'username.require'           => '手机号不能为空',
            'username.unique'           => '该手机号已注册',
            'username'           => '请输入正确的手机号',
            'code  .require'              => '验证码不能为空',
            'password.require'    => '密码不能为空',
            'password.length'    => '密码长度为6到15位',
            'password.confirm'    => '两次密码不一致',
        ];
        $validate = new Validate($rule, $msg);
        if(!$validate->check($post))  return ajaxReturn($validate->getError(),99);
        //验证code值是否正确
        $codeCheack = Db::name('php_sms')->where(['phone' => $post['username'],'code'=> $post['code'],'status'=>0])->find();
        if (!$codeCheack) return ajaxReturn('验证码不正确',99);
        Db::name('php_sms')->where(['id' => $codeCheack['id']])->update(['status' => 1]);
        //验证完成 存入数据库

        unset($post['password_confirm']);
        unset($post['code']);
        $post['password'] = md5($post['password']);
        $dataCheack = Db::name('php_member')->insert($post);
        if(!$dataCheack) return ajaxReturn('注册失败！',0);
        return ajaxReturn('恭喜你注册成功！',1);
    }

    //app忘记密码
    public function forgetPassword(){
        $post = $_POST;
        //验证数据
        $rule = [
            'username'     => 'require|[1][3-9][0-9]{9}',
            'code'          => 'require',
            'password'     => 'require|length:6,15|confirm',
        ];
        $msg = [
            'username.require'      => '手机号不能为空',
            'username'               => '请输入正确的手机号',
            'code  .require'        => '验证码不能为空',
            'password.require'      => '密码不能为空',
            'password.length'       => '密码长度为6到15位',
            'password.confirm'      => '两次密码不一致',
        ];
        $validate = new Validate($rule, $msg);
        if(!$validate->check($post))  return ajaxReturn($validate->getError(),99);
        //判断电话号码是否注册
        $memberCheck = Db::name('php_member')->where(['username' => $post['username']])->find();
        if(!$memberCheck) return ajaxReturn('该电话号码尚未注册',99);
        //查看当前用户是否被禁用
        if($memberCheck['status']) return ajaxReturn('账号已被冻结',99);
        //密码加密
        $post['password'] = md5($post['password']);
        //判断新密码和旧密码是否相同
        if($post['password'] == $memberCheck['password']) return ajaxReturn('新密码和旧密码相同',99);
        //验证code值是否正确
        $codeCheack = Db::name('php_sms')->where(['phone' => $post['username'],'code'=> $post['code'],'status'=>0])->find();
        if (!$codeCheack) return ajaxReturn('验证码不正确',99);
        Db::name('php_sms')->where(['id' => $codeCheack['id']])->update(['status' => 1]);

        //验证完成 存入数据库
        $dataCheack = Db::name('php_member')->where(['id' => $memberCheck['id']])->update(['password' => $post['password']]);
        if(!$dataCheack) return ajaxReturn('找回失败！',0);
        return ajaxReturn('恭喜你修改成功！',1);
    }

    //获取首页接口
    public function getcategory(){
        //获取轮播图
        $ppt = Db::name('php_ads')->where(['status' => 1])->select();
        //获取首页广告标题
        $xinwen = Db::name('php_articles')->where(['status' => 1,'cid' => 8])->select();
        $data = [
            'ppt'=>$ppt,
            'xinwen' => $xinwen
        ];
        return ajaxReturn('成功！',1,$data);
    }

    //获取新闻详情接口
    public function getList(){
        $data = $_GET;
        if(!isset($data['id'])) return ajaxReturn('缺少id',99);
        if($data['can'] == 'tongzhi'){
            //获取系统通知
            $listInfo = Db::name('php_tongzhi')->where(['id' => $data['id']])->find();
            Db::name('php_tongzhi')->where(['id' => $data['id']])->update(['status' => 1]);
        }
        if($data['can'] == 'article'){
            //获取首页新闻和新人帮助
            $listInfo = Db::name('php_articles')->where(['id' => $data['id']])->find();
        }
        if(!$listInfo) return ajaxReturn('未找到该详情',99);
        return ajaxReturn('获取成功',1,$listInfo);
    }

    //获取圈子详情
    public function getquan(){
        $get = $_GET;
        if(!isset($get['page']))  return ajaxReturn('参数错误',99);
        if($get['page'] == 1){
            $headerImage = WEB_URL.'/'.getImgSrc("quanzhidingbutupian");
        }else{
            $headerImage = '';
        }
        $data = Db::name('php_quanzhi')
            ->alias('a')->join('php_member b','a.uid = b.id')
            ->field('a.*,b.nickname,b.image as userimage')
            ->where(['a.status' => 0])->order('a.id desc')->page($get['page'],10)->select();

        //将查询出来的数据里面的图片 改为数组
        foreach ($data as $k => $v){
            if($v['image']){
                $data[$k]['imageall'] = explode('|',$v['image']);
                foreach ($data[$k]['imageall'] as $key=>$value){
                    $data[$k]['imageall'][$key] = WEB_URL.'/'.$value;
                }
            }
            $data[$k]['userimage'] = WEB_URL.'/'.$v['userimage'];
        }
        if(!is_array($data)) return ajaxReturn('错误',99);
        $res = [
          'data' =>   $data,
            'headerImage' => $headerImage
        ];
        if(count($data) > 0){
            return ajaxReturn('获取成功',1,$res);
        }else{
            return ajaxReturn('这个已经是最后一页啦',2);
        }
    }


}